<?

if (!defined("SAPID_STARTED")) die("Hacking attempt!");

if (isset($_POST["post_site"]) and stripslashes($_POST["post_site"]["new_xml"]) and isset($_SESSION["user"]["LOGIN"])) {

	$tree->build_structure();
	if ($_POST["recylceId"] and $DEMO_MODE!="enabled") {
		$d_ids=explode(",", $_POST["recylceId"]);

		function get_node_children($node){
			$ids[]=$node["ID"];
			if(is_array($node["CHILDREN"]))
			foreach ($node["CHILDREN"] as $var=>$id) {
				$ids=array_merge($ids, get_node_children($node[$var]));
			}
			return $ids;
		}

		foreach ($d_ids as $delete_id) {
			foreach ($tree->map as $page) {
				if ($page["ID"]==trim($delete_id)) {
					eval("\$node=\$tree->structure" . $page["POINTER"] . "; unset(\$tree->structure" . $page["POINTER"] . ");");
				}
			}

			if (is_array($node)) {
				$ids=get_node_children($node);
				foreach ($ids as $id) $id_pattern.=$id . "|";
				$id_pattern=preg_replace("/\|$/", "", $id_pattern);
				$d = dir($root_path . "usr/xml/content/");
				while (false !== ($entry = $d->read())) {
					if ($entry!="." and $entry!=".." and preg_match("/^(".$id_pattern.")\.xml/is", $entry)) {
						@unlink($root_path . "usr/xml/content/" . $entry);
					}
				}
				$d->close();
			}
		}
	}

	ignore_user_abort(true);
	
	$outputXML = str_replace("\r\n", "\n", str_replace("<?xml version=\"1.0\"?>", "<?xml version=\"1.0\" encoding=\"utf-8\"?>", stripslashes(preg_replace_callback("/%u([0-9ABCDEF]{4})/", "decode", $_POST["post_site"]["new_xml"]))));
	if(!validateXML($outputXML)) { trigger_error("Can not save structure changes. Input data contains invalid XML.", E_USER_NOTICE); return false; }
	$fp=@fopen($root_path ."usr/xml/tree.xml", "w+");
	if (!$fp) message_die("Couldn't open </b>" . $root_path ."usr/xml/tree.xml</b> for writing!");
	/*
	fwrite($fp, iconv($default_charset, "utf-8", str_replace("\r\n", "\n", str_replace("<?xml version=\"1.0\"?>", "<?xml version=\"1.0\" encoding=\"utf-8\"?>", stripslashes(preg_replace_callback("/%u([0-9ABCDEF]{4})/", "decode", $_POST["post_site"]["new_xml"]))))));
	*/
	fwrite($fp, $outputXML);
	fclose($fp);
	ignore_user_abort(false);
}

$structure=$tree->build_structure();

$pointer="";
if (is_array($env["argv"])) foreach ($env["argv"] as $arg) $pointer.="[\"" . $arg . "\"]";

if ($pointer=="") {
	$code="\$env[\"page\"]=\$structure".$tree->default_pointer.";";
	$code.="\$env[\"argv\"][0]=".$tree->default_pointer_var.";";
	$code.="\$env[\"page\"][\"ID\"]=".$tree->default_pointer_id.";";
} else $code="\$env[\"page\"]=\$structure[\"root\"]".$pointer.";";

@eval($code);

//Doing recursive page search
function check_virtual ($pointer="", $page=false){
	global $tree, $env;
	if (!$env["page"]) {
		$pointer="";
		foreach ($env["argv"] as $arg) {
			$prev_page=$page;
			$prev_pointer=$pointer;
			$pointer.="[\"" . $arg . "\"]";
			$code="\$page=\$tree->structure[\"root\"]".$pointer.";";
			eval($code);
			if (!is_array($page) and $prev_page["virtual"]["MASK"]) {
				//check for current argv to match given mask
				$exp=preg_replace(array("/#/", "/\?/"), array("\d", "[a-zA-Z]"), $prev_page["virtual"]["MASK"]);
				preg_match("/^" . $exp . "$/", $arg, $matches);
				if ($matches) {
					//Ok, arg mathes given pattern
					eval("\$tree->structure[\"root\"]".$pointer."=\$prev_page[\"virtual\"];  unset(\$tree->structure[\"root\"]".$prev_pointer . "[\"virtual\"]);");
					$env["virtual_path_mask"].=$arg . "/";
					check_virtual($pointer, $page);
				}
			}
		}
	}
}

if (!$env["page"]) {
	check_virtual();
	$code="\$env[\"page\"]=\$tree->structure[\"root\"]".$pointer.";";
	@eval($code);
}


?>